package com.rgs.gateway.authentication.server.controller;


import com.rgs.gateway.authentication.server.entity.po.UserPO;
import com.rgs.gateway.authentication.server.service.IAuthenticationService;
import com.rgs.gateway.authentication.server.service.IResourceService;
import com.rgs.gateway.authentication.server.service.IUserService;
import com.rgs.gateway.authentication.server.wrapper.HttpServletRequestAuthWrapper;
import com.rgs.gateway.common.pojo.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/auth")
public class AuthenticationController {

    @Autowired
    IAuthenticationService authenticationService;

    @Autowired
    private IResourceService resourceService;

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "权限验证", notes = "根据用户appKey，访问的url和method判断用户是否有权限访问")
    @PostMapping(value = "/permission")
    public Result<Boolean> decide(@RequestParam String url, @RequestParam String method, HttpServletRequest request) {
        return authenticationService.decide(new HttpServletRequestAuthWrapper(request, url, method));
    }

    @ApiOperation(value = "查询用户信息", notes = "根据用户appKey，查询用户信息")
    @PostMapping(value = "/getUserInfo")
    public Result<UserPO> secret(@RequestParam String appKey, HttpServletRequest request) {
        return Result.success(userService.getAuthUserInfo(appKey));
    }


    @ApiOperation(value = "刷新资源", notes = "刷新资源")
    @GetMapping(value = "/loadResource")
    public Result<Void> loadResource() {
        resourceService.loadResource();
        return Result.success();
    }
}